package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.mapper.NursingPlanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 护理等级Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id) {
        return getById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel) {
        // 1、分页条件查询
        List<NursingLevel> nursingLevels = nursingLevelMapper.selectNursingLevelList(nursingLevel);
        // 2、收集当前页码的所有护理计划ID
        Set<Long> planIds = nursingLevels.stream().map(NursingLevel::getLplanId).collect(Collectors.toSet());
        // 3、根据护理计划ID集合查询护理计划并转为Map（护理计划ID、护理计划名称），最后为护理等级的 护理计划名称赋值
        List<NursingPlan> nursingPlans = nursingPlanMapper.selectBatchIds(planIds);
        if (ObjectUtil.isNotEmpty(nursingPlans)) {
            Map<Long, String> planMap = nursingPlans.stream().collect(Collectors.toMap(NursingPlan::getId, NursingPlan::getPlanName));
            nursingLevels.forEach(level -> level.setPlanName(planMap.get(level.getLplanId())));
        }
        return nursingLevels;
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
        boolean result = save(nursingLevel);
        if (result) {
            deleteCache();
        }
        return result ? 1 : 0;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        boolean result = updateById(nursingLevel);
        if (result) {
            deleteCache();
        }
        return result ? 1 : 0;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids) {
        boolean result = removeByIds(Arrays.asList(ids));
        if (result) {
            deleteCache();
        }
        return result ? 1 : 0;
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        boolean result = removeById(id);
        if (result) {
            deleteCache();
        }
        return result ? 1 : 0;
    }

    public void deleteCache() {
        redisTemplate.delete(CacheConstants.NURSING_LEVEL_ALL_KEY);
    }

    /**
     * 查询所有护理等级
     *
     * @return
     */
    @Override
    public List<NursingLevel> queryAll() throws InterruptedException {
        // 1、先查询Redis的缓存数据
        List<NursingLevel> nursingLevels = (List<NursingLevel>) redisTemplate.opsForValue().get(CacheConstants.NURSING_LEVEL_ALL_KEY);

        // 2、判断缓存是否有数据，如果有，直接返回
        if (ObjectUtil.isNotEmpty(nursingLevels)) {
            return nursingLevels;
        }

        // 3、缓存中没有数据，查询数据库
        nursingLevels = this.lambdaQuery()
                .eq(NursingLevel::getStatus, 1)
                .orderByDesc(NursingLevel::getCreateTime)
                .list();

        // 4、将数据库查询内容放入缓存
        redisTemplate.opsForValue().set(CacheConstants.NURSING_LEVEL_ALL_KEY, nursingLevels);
        return nursingLevels;
    }
}
